package view.report;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import main.WinkelApplication;
import model.DayRevenue;
import view.chart.BarChart;
import view.chart.Chart;
import view.chart.ChartValue;
import view.chart.GraphChart;
import view.component.DateCombobox;

/**
 *
 * @author Dennis Nijssen
 */
public class RevenuePanel extends JPanel 
{    
    private Chart chart;    
    private DateCombobox fromDate;
    private DateCombobox toDate;
    private ArrayList<Integer> usedColors;
    
    private JTable revenueTable;
    private DefaultTableModel model;
    
    public RevenuePanel() 
    {
        super();
        
        this.usedColors = new ArrayList<Integer>();
        
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        
        JLabel titel = new JLabel("Omzet per tijdseenheid", SwingConstants.CENTER);
        titel.setFont(WinkelApplication.computeFontSize(2));
        
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        add(titel, c);
        
        c.gridx = 0;
        c.gridy = 1;
        add(getSearchBar(), c);
        
        model = new DefaultTableModel();
        c.gridx = 0;
        c.gridy = 2;
        //c.weightx = 0.4;
        c.weighty = 1;
        c.gridwidth = 1;
        add(getScrollPaneWithTable(), c);
        
        chart = new BarChart(10);
        c.gridx = 1;
        c.gridy = 2;
        c.weightx = 1;
        c.weighty = 1;
        add(chart, c);
    }
    
    private JPanel getSearchBar() 
    {
        JPanel pnlSearch = new JPanel();
        
        JLabel lblFrom = new JLabel("Van");
        pnlSearch.add(lblFrom);
        
        fromDate = new DateCombobox();        
        pnlSearch.add(fromDate);
        
        JLabel lblTo = new JLabel("Tot");
        pnlSearch.add(lblTo);
        
        toDate = new DateCombobox();
        pnlSearch.add(toDate);
        
        JButton btnSearch = new JButton("Zoeken");
        btnSearch.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                updateChartAndTable(sdf.format(fromDate.getDate()), sdf.format(toDate.getDate()));
            }
        });
        
        pnlSearch.add(btnSearch);
        
        return pnlSearch;
    }
    
    private JScrollPane getScrollPaneWithTable() {
        model.addColumn("Datum");
        model.addColumn("Omzet");
        model.addColumn("Kleur");
        
        // Create the table
        revenueTable = new JTable(model) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if(column == 2) { 
                    c.setBackground((Color)getValueAt(row, column));
                    c.setForeground((Color)getValueAt(row, column));
                } else { 
                    // Back to default for other cells
                    c.setBackground(getBackground());
                    c.setForeground(getForeground());
                }
                
                return c;
            }
        };
        
        return new JScrollPane(revenueTable);
    }
    
    private void updateChartAndTable(String fromDate, String toDate) 
    {
        List<DayRevenue> revenue = WinkelApplication.getQueryManager().getRevenueBetween(fromDate, toDate);
        ChartValue[] values = new ChartValue[revenue.size()];
        int index = 0;
        
        // Clear all used colors
        usedColors.clear();
        
        // Clear table
        model.setRowCount(0);
        
        for (DayRevenue dr : revenue) 
        {
            // Chart label will show as 11-dec-2011
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
            String label = sdf.format(dr.getDate());
            
            Color barColor = getRandomColor();
            
            model.addRow(new Object[] { 
                label,
                WinkelApplication.PRICE_FORMAT.format(dr.getRevenue()),
                barColor
            });
            
            values[index++] = new ChartValue(label, barColor, (int)dr.getRevenue());
        }
        
        chart.setValues(values);
    }
    
    private Color getRandomColor() 
    {
        int colorValue = (int)(Math.random() * 0xFFFFFF);

        while (usedColors.contains(colorValue))
            colorValue = (int)(Math.random() * 0xFFFFFF);

        usedColors.add(colorValue);
        
        return new Color(colorValue);
    }
}